/**
 * 初始化详情对话框
 */
var TopMenuInfoDlg = {
    topMenuInfoData: {},
    ztreeInstance: null,
    editor: null,
    validateFields: {
    	name: {
            validators: {
                notEmpty: {
                    message: '名称不能为空'
                }
            }
        },
        code: {
            validators: {
                notEmpty: {
                    message: '编码不能为空'
                }
            }
        },
        parentid: {
            validators: {
                notEmpty: {
                    message: '父栏目不能为空'
                }
            }
        },
        jumpType: {
            validators: {
                notEmpty: {
                    message: '跳转类型不能为空'
                }
            }
        },
        pri: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
TopMenuInfoDlg.clearData = function () {
    this.topMenuInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TopMenuInfoDlg.set = function (key, value) {
    this.topMenuInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TopMenuInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TopMenuInfoDlg.close = function () {
    parent.layer.close(window.parent.TopMenu.layerIndex);
}

/**
 * 收集数据
 */
TopMenuInfoDlg.collectData = function () {
    this.topMenuInfoData['content'] = TopMenuInfoDlg.editor.txt.html();
    this.set('id').set('name').set('pri').set('code').set('pri').set('parentid').set('jumpType').set('updateUrlFlag');
}

/**
 * 验证数据是否为空
 */
TopMenuInfoDlg.validate = function () {
    $('#topMenuInfoForm').data("bootstrapValidator").resetForm();
    $('#topMenuInfoForm').bootstrapValidator('validate');
    return $("#topMenuInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加
 */
TopMenuInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (isNaN(this.topMenuInfoData.pri)) {
        Feng.error('排序值请输入数字！');
        return;
    }

    //console.log(JSON.stringify(this.topMenuInfoData));
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/hp/menu/add/top", function (data) {
        Feng.success("添加成功!");
        window.parent.TopMenu.table.refresh();
        TopMenuInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.topMenuInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
TopMenuInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    if (isNaN(this.topMenuInfoData.pri)) {
        Feng.error('排序值请输入数字！');
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/hp/menu/update", function (data) {
        Feng.success("修改成功!");
        window.parent.TopMenu.table.refresh();
        TopMenuInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.topMenuInfoData);
    ajax.start();
}


/**
 * 选择框值变化时
 */
TopMenuInfoDlg.onSelectChange = function () {
    var sValue = $("#jumpType").val();
    if(sValue == 'details'){
        $("#editorForm").show();
    }else{
        $("#editorForm").hide();
    }
};

/**
 * 点击父级编号input框时
 */
TopMenuInfoDlg.onClickDept = function (e, treeId, treeNode) {
    $("#parentName").attr("value", TopMenuInfoDlg.ztreeInstance.getSelectedVal());
    $("#parentid").attr("value", treeNode.id);
};


/**
* 显示父级菜单选择的树
*/
TopMenuInfoDlg.showMenuSelectTree = function () {
    Feng.showInputTree("parentName", "parentidTreeDiv", 15, 34);
};

$(function () {
    Feng.initValidator("topMenuInfoForm", TopMenuInfoDlg.validateFields);

    var ztree = new $ZTree("parentidTree", "/hp/menu/selectMenuTreeList");
    ztree.bindOnClick(TopMenuInfoDlg.onClickDept);
    ztree.init();
    TopMenuInfoDlg.ztreeInstance = ztree;

    //初始化是否展示选项
    $("#jumpType").val($("#jumpTypeVal").val());
    $("#jumpType").change(TopMenuInfoDlg.onSelectChange);

    if($("#jumpType").val() == 'details'){
        $("#editorForm").show();
    }

    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    //editor.customConfig.uploadImgShowBase64 = true;
    editor.customConfig.uploadImgServer = '/hp/pic/wangeditorUpload';
    editor.customConfig.uploadFileName = 'picture';
    editor.create();
    editor.txt.html($("#contentVal").val());
    TopMenuInfoDlg.editor = editor;
});
